import GroupList from "./src/GroupList.vue";
import GroupCreator from "./src/GroupCreator.vue";
import GroupEditor from "./src/GroupEditor.vue";
import { currentApp, getSeed } from "@/views/seed-dev";
import { MicroConfiguration } from "@/views/dev-types";
export { GroupList, GroupCreator, GroupEditor }

export function getInitData(seedName?: string) {
  const ret = []
  const groups = seedName ? getSeed(seedName).groups : currentApp.value.groups
  if(!groups) return ret

  for(const groupName in groups) ret.push({ ...groups[groupName], name: groupName })
  return ret
}

export function getAppOptions() {
  const ret = toTreeData(currentApp.value)

  const micros = currentApp.value.micros
  if(micros) {
    for(const microName in micros) {
      const micro = micros[microName]
      const children = toTreeData(micro, microName)
      if(children.length > 0) ret.push({id: microName,label: micro.label,type: 'micro',icon: 'ep:grid', children})
    }
  }

  return ret
}

function toTreeData(micro: MicroConfiguration, microName?: string) {
  const ret = []

  const pre = microName ? microName + '.': ''
  const actions = micro.actions
  if(actions) {
    for(const actionName in actions) {
      const action = actions[actionName]
      ret.push({id: pre + actionName,label: action.label,type: 'action',icon: 'fluent-mdl2:set-action'})
    }
  }

  const homes = micro.homes
  if(homes) {
    for(const homeName in homes) {
      const home = homes[homeName]
      ret.push({id: pre + homeName, label: home.label, type: 'home', icon: 'ic:outline-home', color: '#909399'})
    }
  }

  const seeds = micro.seeds
  if(!seeds) return ret
  
  for(const seedName in seeds) {
    const seed = seeds[seedName]
    ret.push({id: pre + seedName, label: seed.label, type: 'seed', icon: 'streamline-ultimate-color:module-hands-puzzle'})
  }

  return ret
}